<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教室申请代码" prop="caId">
        <el-input
          v-model="queryParams.caId"
          placeholder="请输入教室申请代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教室名称" prop="caName">
        <el-input
          v-model="queryParams.caName"
          placeholder="请输入教室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="caTime">
        <el-input
          v-model="queryParams.caTime"
          placeholder="请输入申请时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="caProposer">
        <el-input
          v-model="queryParams.caProposer"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借用开始时间" prop="caBorrowStartTime">
        <el-input
          v-model="queryParams.caBorrowStartTime"
          placeholder="请输入借用开始时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借用目的" prop="caPurpose">
        <el-input
          v-model="queryParams.caPurpose"
          placeholder="请输入借用目的"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="容纳人数" prop="caNumber">
        <el-input
          v-model="queryParams.caNumber"
          placeholder="请输入容纳人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借用时间" prop="caBorrowTime">
        <el-input
          v-model="queryParams.caBorrowTime"
          placeholder="请输入借用时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借用截止时间" prop="caBorrowEndTime">
        <el-input
          v-model="queryParams.caBorrowEndTime"
          placeholder="请输入借用截止时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:apply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:apply:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:apply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:apply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="教室申请代码" align="center" prop="caId" />
      <el-table-column label="教室名称" align="center" prop="caName" />
      <el-table-column label="申请时间" align="center" prop="caTimeStr" />
      <el-table-column label="申请人" align="center" prop="caProposer" />
      <el-table-column label="审批状态" align="center" prop="caStatus" />
      <el-table-column label="借用开始时间" align="center" prop="caBorrowStartTimeStr" />
      <el-table-column label="借用目的" align="center" prop="caPurpose" />
      <el-table-column label="容纳人数" align="center" prop="caNumber" />
      <el-table-column label="借用时间" align="center" prop="caBorrowTime" />
      <el-table-column label="借用截止时间" align="center" prop="caBorrowEndTimeStr" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:apply:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:apply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【申请教室】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="教室申请代码" prop="caId">-->
<!--          <el-input v-model="form.caId" placeholder="请输入教室申请代码" />-->
<!--        </el-form-item>-->
        <el-form-item label="教室名称" prop="caName">
<!--          <el-input v-model="form.caName" placeholder="请输入教室名称" />-->

          <el-select v-model="form.caName" placeholder="请选择教室">
            <el-option
              v-for="item in roomList"
              :key="item.value"
              :label="item.label"
              :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="申请时间" prop="caTime">-->
<!--          <el-input v-model="form.caTime" placeholder="20240617" />-->
<!--        </el-form-item>-->
        <el-form-item label="申请人" prop="caProposer">
          <el-input v-model="form.caProposer" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="借用开始时间" prop="caBorrowStartTime">
<!--          <el-input v-model="form.caBorrowStartTime" placeholder="请输入借用开始时间" />-->
          <el-date-picker
            v-model="form.caBorrowStartTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="借用目的" prop="caPurpose">
          <el-input v-model="form.caPurpose" placeholder="请输入借用目的" />
        </el-form-item>
        <el-form-item label="容纳人数" prop="caNumber">
          <el-input v-model="form.caNumber" placeholder="请输入容纳人数" />
        </el-form-item>
        <el-form-item label="借用时间" prop="caBorrowTime">
          <el-input v-model="form.caBorrowTime" placeholder="按分钟计算"/>
        </el-form-item>
<!--        <el-form-item label="借用截止时间" prop="caBorrowEndTime">-->
<!--          <el-input v-model="form.caBorrowEndTime" placeholder="请输入借用截止时间" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApply, getApply, delApply, addApply, updateApply } from "@/api/system/apply";
import { listClassroomMsg } from "@/api/system/classroom";

export default {
  name: "Apply",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【申请教室】表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        caId: null,
        caName: null,
        caTime: null,
        caProposer: null,
        caStatus: null,
        caBorrowStartTime: null,
        caPurpose: null,
        caNumber: null,
        caBorrowTime: null,
        caBorrowEndTime: null
      },
      // 表单参数
      form: {},
      //教室下拉属性数组
      roomList:[],
      // 表单校验
      rules: {
        caId: [
          { required: true, message: "教室申请代码不能为空", trigger: "blur" }
        ],
        caName: [
          { required: true, message: "教室名称不能为空", trigger: "blur" }
        ],
        caTime: [
          { required: true, message: "申请时间不能为空", trigger: "blur" }
        ],
        caProposer: [
          { required: true, message: "申请人不能为空", trigger: "blur" }
        ],
        caStatus: [
          { required: false, message: "审批状态不能为空", trigger: "change" }
        ],
        caPurpose: [
          { required: true, message: "借用目的不能为空", trigger: "blur" }
        ],
        caNumber: [
          { required: false, message: "容纳人数不能为空", trigger: "blur" }
        ],
        caBorrowTime: [
          { required: true, message: "借用时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.listClassroomMsg();
  },
  methods: {
    /** 查询【申请教室】列表 */
    getList() {
      this.loading = true;
      listApply(this.queryParams).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    listClassroomMsg() {
      this.loading = true;
      listClassroomMsg().then(response => {
        this.roomList=response.data;
        console.log(this.roomList)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        caId: null,
        caName: null,
        caTime: null,
        caProposer: null,
        caStatus: null,
        caBorrowStartTime: null,
        caPurpose: null,
        caNumber: null,
        caBorrowTime: null,
        caBorrowEndTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【申请教室】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【申请教室】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //caBorrowStartTime
          console.log( this.form)
           this.form.caBorrowStartTime=Date.parse(this.form.caBorrowStartTime)
          if (this.form.id != null) {
            updateApply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【申请教室】编号为"' + ids + '"的数据项？').then(function() {
        return delApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/apply/export', {
        ...this.queryParams
      }, `apply_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
